<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="140px"
    >
      <el-form-item
        label="用户名"
        prop="username"
      >
        <el-input
          v-model="dataForm.username"
          required 
          placeholder="用户名"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="密码"
        prop="password"
      >
        <el-input
          v-model="dataForm.password"
          required 
          placeholder="密码"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="昵称"
        prop="nickname"
      >
        <el-input
          v-model="dataForm.nickname"
          required 
          placeholder="昵称"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="电话"
        prop="phone"
      >
        <el-input
          v-model="dataForm.phone"
          required 
          placeholder="电话"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="邮箱"
        prop="email"
      >
        <el-input
          v-model="dataForm.email"
          required 
          placeholder="邮箱"
        ></el-input>
      </el-form-item>

      <el-form-item
        label="头像"
        prop="header"
      >
        <single-upload v-model="dataForm.header"></single-upload>
      </el-form-item>
      <el-form-item 
        label="性别"
        prop="gender"
      >
        <template>
          <el-radio-group v-model="dataForm.gender">
          <el-radio :label="1">男</el-radio>
          <el-radio :label="0">女</el-radio>
          </el-radio-group>
        </template>
      </el-form-item>

      <el-form-item 
        label="生日"
        prop="birthday"
      >
        <el-date-picker 
        v-model="dataForm.birthday" 
        type="date" 
        placeholder="请选择您的生日">
        </el-date-picker>
      </el-form-item>

      <el-form-item
        label="显示状态"
        prop="status"
      >
        <el-switch
          v-model="dataForm.status"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :active-value="1"
          :inactive-value="0"
        ></el-switch>
      </el-form-item>
    </el-form>
    <span
      slot="footer"
      class="dialog-footer"
    >
      <el-button @click="visible = false">取消</el-button>
      <el-button
        type="primary"
        @click="dataFormSubmit()"
      >确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import SingleUpload from './singleUpload'
export default {
  components: { SingleUpload },
  data () {
    return {
      visible: false,
      roleList: [],
      dataForm: {
        id: 0,
        username: '',
        password: '',
        nickname: '',
        phone: '',
        email: '',
        gender: '',
        birthday: '',
        header: '',
        status: 1,
        createTime: '',
        newTime: ''
      },
      dataRule: {
        username: [{ required: true, message: '用户名不能为空', trigger: 'blur' }],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' }
        ],
        nickname: [
          { required: true, message: '请输入您的昵称', trigger: 'blur' }
        ],
        birthday: [
          { required: false, message: '请选择您的出生日期', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          {
            type: 'email',
            message: '请输入正确的邮箱地址',
            trigger: ['blur', 'change']
          }
        ],
        phone: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          {
            pattern: /^((0\d{2,3}-\d{7,8})|(1[3584]\d{9}))$/,
            message: '请输入正确的手机号码或者座机号'
          }
        ],
        header: [
          { required: true, message: '头像地址不能为空', trigger: 'blur' }
        ],
        gender: [
          { required: true, message: '请选择您的性别', trigger: 'blur' }
        ],
        status: [
          {
            required: true,
            message: '显示状态[0-不显示；1-显示]不能为空',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    // getCurrentTime () {
    //   this.newTime = new Date()
    //   console.log(this.newTime)
    // },
    init (id) {
      this.dataForm.id = id || 0
      this.$http({
        url: this.$http.adornUrl('/sys/role/list'),
        method: 'get',
        params: this.$http.adornParams()
      }).then(({data}) => {
        this.roleList = data.page.list
      }).then(() => {
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
      }).then(() => {
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/user/user/info/${this.dataForm.id.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.username = data.user.username
              this.dataForm.password = data.user.password
              this.dataForm.nickname = data.user.nickname
              this.dataForm.phone = data.user.phone
              this.dataForm.email = data.user.email
              this.dataForm.birthday = data.user.birthday
              this.dataForm.gender = data.user.gender
              this.dataForm.header = data.user.header
              this.dataForm.status = data.user.status
              this.dataForm.createTime = data.user.createTime
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      // this.$options.methods.getCurrentTime()
      this.newTime = new Date()
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/user/user/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id.id || undefined,
              'username': this.dataForm.username,
              'password': this.dataForm.password,
              'nickname': this.dataForm.nickname,
              'phone': this.dataForm.phone,
              'birthday': this.dataForm.birthday,
              'gender': this.dataForm.gender,
              'email': this.dataForm.email,
              'header': this.dataForm.header,
              'status': this.dataForm.status,
              'createTime': !this.dataForm.id ? this.newTime : this.dataForm.createTime
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              console.log({
                'id': this.dataForm.id.id || undefined,
                'username': this.dataForm.username,
                'password': this.dataForm.password,
                'nickname': this.dataForm.nickname,
                'phone': this.dataForm.phone,
                'birthday': this.dataForm.birthday,
                'gender': this.dataForm.gender,
                'email': this.dataForm.email,
                'header': this.dataForm.header,
                'status': this.dataForm.status,
                'createTime': !this.dataForm.id ? this.newTime : this.dataForm.createTime
              })
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
